<template>
  <div class="ratings" ref="ratings">
    <div class="ratings-content">
      <!-- 商家部分 -->
      <div class="overview">
        <div class="overview-left">
          <h1 class="score">{{info.score}}</h1>
          <div class="title">综合评分</div>
          <div class="rank">高于周边商家 99%</div>
        </div>
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <!-- 评分 -->
            <Star :score="info.serviceScore" :size="36" />
            <span class="score">{{info.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <Star :score="info.foodScore" :size="36" />
            <span class="score">{{info.foodScore}}</span>
          </div>
          <div class="delivery-wrapper">
            <span class="title">送达时间</span>
            <span class="delivery">{{info.deliveryTime}} 分钟</span>
          </div>
        </div>
      </div>
      <div class="split"></div>
      <div class="ratingselect">
        <div class="rating-type border-1px">
          <!-- 交互 -->
          <span
            class="block positive"
            :class="{ active: selectType === 2}"
            @click="setSelectType(2)"
          >
            全部
            <span class="count">{{ratings.length}}</span>
            <!-- 直接通过评估的总长度来获取 满意的是为0 的就拿出来 不满意就相减得到 -->
          </span>
          <span
            class="block positive"
            :class="{ active: selectType === 0}"
            @click="setSelectType(0)"
          >
            满意
            <span class="count">{{positiveSize}}</span>
            <!-- 这里根据一个计算属性来统计rateType为0 的就是+1 veux管理 -->
          </span>
          <span
            class="block negative"
            :class="{ active: selectType === 1}"
            @click="setSelectType(1)"
          >
            不满意
            <span class="count">{{ratings.length - positiveSize}}</span>
          </span>
        </div>
        <!-- 是否为true 看on类名的这里没有复选框 点击切换 -->
        <div class="switch" :class="{on:onlyShowText}" @click="toggleOnlyShowText">
          <span class="iconfont icon-check_circle"></span>
          <span class="text">只看有内容的评价</span>
        </div>
      </div>
      <div class="rating-wrapper">
        <ul>
          <!-- 评论列表 根据计算属性过渡得到的一个新数组来进行循环数组的 每一项也是有对应的name值得 -->
          <li class="rating-item" v-for="(rating,index) in filterRatings" :key="index">
            <div class="avatar">
              <img width="28" height="28" :src="rating.avatar" />
            </div>
            <div class="content">
              <h1 class="name">{{rating.username}}</h1>
              <div class="star-wrapper">
                <Star :score="5" :size="24" />
                <span class="delivery">{{rating.deliveryTime}}</span>
              </div>
              <p class="text">{{rating.text}}</p>
              <div class="recommend">
                <!-- 0赞 1 👎 三目判断-->
                <span
                  class="iconfont"
                  :class="rating.rateType===0 ? 'icon-thumb_up' : 'icon-thumb_down'"
                ></span>
                <span class="item" v-for="(item,index) in rating.recommend" :key="index">{{item}}</span>
              </div>
              <div class="time">{{rating.rateTime | data-format}}</div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Star from "components/Star/Stars.vue";
import BScroll from "better-scroll";
import { mapState, mapGetters } from "vuex";
export default {
  name: "",
  data() {
    return {
      //需要的相关数据做显示不同的评价内容
      onlyShowText: true, // 是否只显示有文本的 默认是true 显示文本
      selectType: 2, // 选择的评价类型: 0满意, 1不满意, 2全部
    };
  },
  mounted() {
    this.$store.dispatch("getShopRatings", () => {
      //有数据到的时候才执行这个回调函数 之后 new一个滚动对象出来 就号了
      this.$nextTick(() => {
        //给整个列表实现滚动
        new BScroll(this.$refs.ratings, {
          click: true,
        });
      });
    });
  },
  components: {
    Star,
  },
  computed: {
    ...mapState(["info", "ratings"]),
    ...mapGetters(["positiveSize"]),
    //过滤新的评价数组列表进行交互效果
    filterRatings() {
      /**找出关联的参数就是根据什么才能过滤出来  返回条件true
       * params: onlyShowText  selectType ratings rateType text
       * return Aaary
       */
      const { onlyShowText, selectType, ratings } = this;
      //  属性多条件过滤 需要分析全部的因素才行 通过filter方法过滤返回新数组 条件为true就返回
      return ratings.filter((ratings) => {
        const { rateType, text } = ratings; //根据这2个条件判断 012和显示不显示文本
        /* 存在逻辑中断 && || 
          条件1 :
            selectType === 2的时候 就是显示全部了 不必在去找满意不满意了
            selectType === rateType 决定显示满意还是不满意 过滤出对应的数组
            selectType: 0/1/2
            rateType: 0/1
            selectType===2 || selectType===rateType  满足其一就好

          条件2:
            onlyShowText 传递进来的true默认嘛取反就是fasle  返回表达式2 那就是显示有文本的 
            onlyShowText 传递过滤的是false 取反后 那就显示有或者没有文本的 后面的text就不做考虑
            || 左边不满足就去看右边 满足返回表达式2 
              左边满足就立刻执行了返回表达式1  右边就不执行了
         */
        return (
          (selectType === 2 || selectType === rateType) &&
          (!onlyShowText || text)
        ); //这是是返回true的 满足这个条件就返回新数组 二边都为true才行
        /*(!onlyShowText || onlyShowText && text) 这里就不用这样写了 || &&同时存在时候&&优先 之后在比较|| 满足其一就为true 二边都是false的时候才是fasle
            && 左边不满足直接返回表达式1 后面不在执行 
              左边满足就立刻执行了返回表达式2   二者满足才为true  
              以上项目是返回布尔值 不是表达式 只是做个逻辑分析
        */
      });
    },
  },
  methods: {
    //控制tab切换不用的内容  3个类型公用一个方法 或者自己click = selectyep = 1 直接赋值
    setSelectType(selectType) {
      this.selectType = selectType;
    },
    //控制显示和不显示文本评价内容
    toggleOnlyShowText() {
      this.onlyShowText = !this.onlyShowText;
    },
  },
};
</script>

<style lang="stylus" rel="stylesheet/stylus">
@import '../../../common/stylus/mixins';

.ratings {
  position: absolute;
  top: 195px;
  bottom: 0;
  left: 0;
  width: 100%;
  overflow: hidden;
  background: #fff;

  .overview {
    display: flex;
    padding: 18px 0;

    .overview-left {
      flex: 0 0 137px;
      padding: 6px 0;
      width: 137px;
      border-right: 1px solid rgba(7, 17, 27, 0.1);
      text-align: center;

      @media only screen and (max-width: 320px) {
        flex: 0 0 120px;
        width: 120px;
      }

      .score {
        margin-bottom: 6px;
        line-height: 28px;
        font-size: 24px;
        color: rgb(255, 153, 0);
      }

      .title {
        margin-bottom: 8px;
        line-height: 12px;
        font-size: 12px;
        color: rgb(7, 17, 27);
      }

      .rank {
        line-height: 10px;
        font-size: 10px;
        color: rgb(147, 153, 159);
      }
    }

    .overview-right {
      flex: 1;
      padding: 6px 0 6px 24px;

      @media only screen and (max-width: 320px) {
        padding-left: 6px;
      }

      .score-wrapper {
        margin-bottom: 8px;
        font-size: 0;

        .title {
          display: inline-block;
          line-height: 18px;
          vertical-align: top;
          font-size: 12px;
          color: rgb(7, 17, 27);
        }

        .star {
          display: inline-block;
          margin: 0 12px;
          vertical-align: top;
        }

        .score {
          display: inline-block;
          line-height: 18px;
          vertical-align: top;
          font-size: 12px;
          color: rgb(255, 153, 0);
        }
      }

      .delivery-wrapper {
        font-size: 0;

        .title {
          line-height: 18px;
          font-size: 12px;
          color: rgb(7, 17, 27);
        }

        .delivery {
          margin-left: 12px;
          font-size: 12px;
          color: rgb(147, 153, 159);
        }
      }
    }
  }

  .split {
    width: 100%;
    height: 16px;
    border-top: 1px solid rgba(7, 17, 27, 0.1);
    border-bottom: 1px solid rgba(7, 17, 27, 0.1);
    background: #f3f5f7;
  }

  .ratingselect {
    .rating-type {
      padding: 18px 0;
      margin: 0 18px;
      // border-1px(rgba(7, 17, 27, 0.1));
      font-size: 0;

      .block {
        display: inline-block;
        padding: 8px 12px;
        margin-right: 8px;
        line-height: 16px;
        border-radius: 1px;
        font-size: 12px;
        color: rgb(77, 85, 93);
        background: rgba(77, 85, 93, 0.2);

        &.active {
          background: $green;
          color: #fff;
        }

        .count {
          margin-left: 2px;
          font-size: 8px;
        }
      }
    }

    .switch {
      padding: 12px 18px;
      line-height: 24px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      color: rgb(147, 153, 159);
      font-size: 0;

      &.on {
        .icon-check_circle {
          color: $green;
        }
      }

      .icon-check_circle {
        display: inline-block;
        vertical-align: top;
        margin-right: 4px;
        font-size: 24px;
      }

      .text {
        display: inline-block;
        vertical-align: top;
        font-size: 12px;
      }
    }
  }

  .rating-wrapper {
    padding: 0 18px;

    .rating-item {
      display: flex;
      padding: 18px 0;
      bottom-border-1px(rgba(7, 17, 27, 0.1));

      .avatar {
        flex: 0 0 28px;
        width: 28px;
        margin-right: 12px;

        img {
          border-radius: 50%;
        }
      }

      .content {
        position: relative;
        flex: 1;

        .name {
          margin-bottom: 4px;
          line-height: 12px;
          font-size: 10px;
          color: rgb(7, 17, 27);
        }

        .star-wrapper {
          margin-bottom: 6px;
          font-size: 0;

          .star {
            display: inline-block;
            margin-right: 6px;
            vertical-align: top;
          }

          .delivery {
            display: inline-block;
            vertical-align: top;
            line-height: 12px;
            font-size: 10px;
            color: rgb(147, 153, 159);
          }
        }

        .text {
          margin-bottom: 8px;
          line-height: 18px;
          color: rgb(7, 17, 27);
          font-size: 12px;
        }

        .recommend {
          line-height: 16px;
          font-size: 0;

          .icon-thumb_up, .icon-thumb_down, .item {
            display: inline-block;
            margin: 0 8px 4px 0;
            font-size: 9px;
          }

          .icon-thumb_up {
            color: $yellow;
          }

          .icon-thumb_down {
            color: rgb(147, 153, 159);
          }

          .item {
            padding: 0 6px;
            border: 1px solid rgba(7, 17, 27, 0.1);
            border-radius: 1px;
            color: rgb(147, 153, 159);
            background: #fff;
          }
        }

        .time {
          position: absolute;
          top: 0;
          right: 0;
          line-height: 12px;
          font-size: 10px;
          color: rgb(147, 153, 159);
        }
      }
    }
  }
}
</style>
